package com.ssm.util.test.json;

import java.util.Map;

import org.junit.Test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ssm.util.test.collection.JsonMap;
/**
 * 使用fastjson.jar解析json数据
 * @author Administrator
 *
 */
public class ParseJson {
	//解析json数据获取指定字段
	@Test
	public void test1() {
		//alibaba fastjson
		String json="{\"code\":\"0000\",\"hmac\":\"a26eb1e435758e4bfbcde6236b85f7e6\",\"message\":\"成功\",\"splits\":[{\"list\":[{\"amount\":\"0.0\",\"orderNo\":\"724154877214982144\",\"splitCustomerNumber\":\"10021015783\",\"splitStatus\":\"SUCCESS\",\"splitTime\":\"2018-08-16 15:50:38.618528\"}],\"splitType\":\"TRADE_SPLITTER\",\"totalAmount\":\"0.0\"}],\"totalRecords\":1,\"tradeReceives\":[{\"amount\":\"1201\",\"bankCode\":\"GDB\",\"busiType\":\"COMMON\",\"createTime\":\"2018-08-16 15:48:38\",\"customerNumber\":\"10024421967\",\"externalId\":\"724154877214982144\",\"fee\":\"5.04\",\"ip\":\"120.24.98.96\",\"lastNo\":\"625810******3374\",\"payTime\":\"2018-08-16 15:50:25\",\"payerName\":\"曾*龙\",\"payerPhone\":\"135****2707\",\"productVersion\":\"\",\"requestId\":\"81808161541100145994\",\"src\":\"B\",\"status\":\"SUCCESS\",\"withdrawStatus\":\"RECEIVED\"}]}";

      	JSONObject jsonObject = JSON.parseObject(json);   //有{}就是JSONObject
      	JSONArray array = (JSONArray)jsonObject.get("tradeReceives"); //有[]就是JSONArray
      	System.out.println(array.toString());
	    for (int i = 0; i < array.size(); i++) {
		    JSONObject jsonObject1 = (JSONObject)array.get(i); //这里i就是等于0，只有一个元素对象
		    Object status = jsonObject1.get("status");//里面字段什么都没有就是Object
		    Object externalId = jsonObject1.get("externalId");//里面字段什么都没有就是Object
		    System.out.println(status.toString());
		    System.out.println(externalId.toString());
	    }
	}
	//json数据转换map
	@Test
	public void test2() {
		String json="{\"code\":\"0000\",\"hmac\":\"a26eb1e435758e4bfbcde6236b85f7e6\",\"message\":\"成功\",\"splits\":[{\"list\":[{\"amount\":\"0.0\",\"orderNo\":\"724154877214982144\",\"splitCustomerNumber\":\"10021015783\",\"splitStatus\":\"SUCCESS\",\"splitTime\":\"2018-08-16 15:50:38.618528\"}],\"splitType\":\"TRADE_SPLITTER\",\"totalAmount\":\"0.0\"}],\"totalRecords\":1,\"tradeReceives\":[{\"amount\":\"1201\",\"bankCode\":\"GDB\",\"busiType\":\"COMMON\",\"createTime\":\"2018-08-16 15:48:38\",\"customerNumber\":\"10024421967\",\"externalId\":\"724154877214982144\",\"fee\":\"5.04\",\"ip\":\"120.24.98.96\",\"lastNo\":\"625810******3374\",\"payTime\":\"2018-08-16 15:50:25\",\"payerName\":\"曾*龙\",\"payerPhone\":\"135****2707\",\"productVersion\":\"\",\"requestId\":\"81808161541100145994\",\"src\":\"B\",\"status\":\"SUCCESS\",\"withdrawStatus\":\"RECEIVED\"}]}";
		Map mapTypes = JSONObject.parseObject(json);
		for (Object obj : mapTypes.keySet()){  
            System.out.println("key为："+obj+"值为："+mapTypes.get(obj));  
        }
		
		String string = JSON.toJSON(mapTypes).toString();
		System.out.println("map转json:"+string);
	}
	//json转换成对象
	@Test
	public void  test3()  throws Exception{
		String text="{\"aaa\":\"111\",\"ccc\":\"333\",\"bbb\":\"222\"}";
		JSONObject json = (JSONObject) JSONObject.parse(text);
		JsonMap map = JSONObject.toJavaObject(json, JsonMap.class);
		System.out.println(map.getAaa());
	}
	
	
	

}
